<?php
// View for admin - pages
require_once WACT_ROOT.'controller/controller.inc.php';
require_once WACT_ROOT .'controller/form.inc.php';
require_once WACT_ROOT.'view/view.inc.php';
require_once WACT_ROOT .'view/formview.inc.php';

require_once APP_ROOT . 'view/common/layout.formview.php';
require_once APP_ROOT . 'view/common/layout.view.php';

require_once 'model/common/user.php';
require_once 'model/common/page.php';
require_once 'model/common/article.php';
require_once 'model/common/categories.php';
require_once 'model/common/date.php';


class PagesListView extends LayoutFormView {

    function PagesListView($TemplateFile) {
        parent :: LayoutFormView($TemplateFile);
    }

    function prepare(&$controller, &$request, &$responseModel) {
    	parent::prepare($controller, $request, $responseModel);
        		        
		$this->Template->set('SITE_CSS',Site::getSiteTemplateCSS());
		$this->Template->set('SITE_BASE',Site::getSiteTemplateBase());
		$this->Template->set('SITE_TITLE',Site::getTitle());
 

		$pager =& $this->Template->getChild('pagenav');
        $pager2 =& $this->Template->getChild('pagenav2');

        

        $list =& Page::getPagesList($pager,$request,$responseModel);
        $list->paginate($pager2);


        $list_updated=array();


        $list->reset();
        while ($list->next()) {

        	$dataspace = $list->export();
            $firstname = $list->get('firstname');
            $lastname = $list->get('lastname');
            $page_id = $list->get('page_id');
            $author_show = $firstname.' '.$lastname;
            $dataspace['author_show'] =  $author_show;
            $type =  $list->get('type');
            $type_show = Page::getTypeName($type);
            $dataspace['type_show'] =  $type_show;

            $premium = $list->get('premium');
            if ($premium) $dataspace['premium_show'] =  1;

            // print_r($dataspace);
            $list_updated[$page_id] = $dataspace;
        }


        $list_source_updated =& new ArrayDataSet($list_updated);
        $this->Template->setChildDataSource('List', $list_source_updated);

        //populate sort combo
        $sortby =& $this->Template->getChild('sort');
        $sort_list = array('page_id'=>'ID','headline'=>'Headline','type'=>'Type','user_id'=>'Author','date_added desc'=>'Date Posted desc' );
        $sortby->setChoices($sort_list);

        //populate recent combo
        $recent =& $this->Template->getChild('recent');
        $recent_list  =& Article::getResentList();
        $recent->setChoices($recent_list);

        //populate months
        $start_month =& $this->Template->getChild('start_month');
        $end_month =& $this->Template->getChild('end_month');
        $months_list =& SportDate::getMonthsList();
        $start_month->setChoices($months_list);
        $end_month->setChoices($months_list);

        //prepopulate start and end date
        $cur_month = (integer)date("m");
        $cur_year = (integer)date("Y");
        $cur_day = (integer)date("d");

        if ( $cur_month==1 ) {
        	$prev_month = 12;
        	$prev_year = $cur_year-1;
        }  else {
           $prev_month = $cur_month - 1;
           $prev_year = $cur_year;
        }

        if ( $cur_month==12 ) {
        	$next_month = 1;
        	$next_year = $cur_year+1;
        }  else {
           $next_month = $cur_month + 1;
           $next_year = $cur_year;
        }

        $start_day =& $this->Template->getChild('start_day');
        $day = $start_day->getValue();
        if (!$day) $start_day->setValue(1);

        $end_day =& $this->Template->getChild('end_day');
        $day = $end_day->getValue();
        if (!$day) $end_day->setValue($cur_day);

        //print $start_month->getValue();
        if ($start_month->getValue()<=0) $start_month->setSelection($prev_month);
        if ($end_month->getValue()<=0) {
        	//print $cur_month;
        	$end_month->setSelection($cur_month);
        }

        $start_year =& $this->Template->getChild('start_year');
        $year = $start_year->getValue();
        if (!$year) $start_year->setValue($prev_year);

        $end_year =& $this->Template->getChild('end_year');
        $year = $end_year->getValue();
        if (!$year) $end_year->setValue($cur_year);

        //populate types
        $types_list =& Page::getTypesList();
        $types =& $this->Template->getChild('type');
        $types->setChoices($types_list);

        //populate authors
        $authors_list =& User::getAuthorsList();
        $author =& $this->Template->getChild('author');
        $author->setChoices($authors_list);


    }

}






?>